package com.example.postmsg;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class HttpRequest {
        /**
         *Post请求
         */
        public String doPost(String url , List<NameValuePair> nameValuePairs){
                //新建HttpClient对象  
                HttpClient httpclient = new DefaultHttpClient();
                //创建POST连接
                HttpPost httppost = new HttpPost(url);
                Log.d("HTTP","before try");
                try {
//                        //使用PSOT方式，必须用NameValuePair数组传递参数
                        
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
                        Log.d("HTTP","before exe");
                        HttpResponse response = httpclient.execute(httppost);
                        Log.d("after exe","after exe");
                        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)  
                        {  
                        	Log.d("HTTPrequest","OK");
                        	String content = EntityUtils.toString(response.getEntity());
                        	return content;	
                        }
                        return "no response";
                } catch (ClientProtocolException e) {
                        e.printStackTrace();
                        Log.d("httprequest","catch1");
                } catch (IOException e) {
                        e.printStackTrace();
                        Log.d("httprequest","catch2");
                }
                Log.d("Main","return null");
                return null;
        }
        
        /**
         *Get请求
         */
        public String doGet(String url, List<NameValuePair> nameValuePairs){
            
            HttpClient httpClient = new DefaultHttpClient();
                // GET
            url += "?"+ URLEncodedUtils.format(nameValuePairs, HTTP.UTF_8);;
            HttpGet httpGet = new HttpGet(url);
            
                try {
                        HttpResponse response = httpClient.execute(httpGet);
                        String content = EntityUtils.toString(response.getEntity());
                        /*if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){
                                Log.i("GET", "Bad Request!");
                        }*/
                        Log.d("content",content);
                        return content;
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return null;
        }
}
